// pages/address/address.js

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
   
    //省市区选择器：
    region: ['请选择', '请选择', '请选择'],
    customItem: '请选择',//为每一列的顶部添加一个自定义的项
    userId:'',//登录用户ID
    status:'add',//状态 add添加 save编辑
    data:[],
    id:''


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this;
      that.setData({
        status:options.status
      });
     //如果是修改
      if(options.status == 'save'){
        var id = options.id
        this.setData({
          id:options.id
        })
        app.wxRequest('GET','/Home/User/show_address',{
          'id':id
        },function(res){
          console.log(res);
          if(res.data.code == 1){
              that.setData({
                data:res.data.data,
                region:[res.data.data.province,res.data.data.city,res.data.data.area]
              })
          }
        })
      }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     //获取登录信息
     var userInfo = wx.getStorageSync("userInfo");
     if (userInfo){
       //console.log(userInfo);
       this.setData({
         userId: userInfo.id
       })
     }
  },

  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //省市区选择器：
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  saveAddress(res){
    var that = this;
    console.log(res);
    var name = res.detail.value.consignee;
    var mobile = res.detail.value.mobile;
    var address = res.detail.value.address; 
    var province = that.data.region[0];
    var city = that.data.region[1];
    var area = that.data.region[2];
    var status = that.data.status;
    var userId = that.data.userId;
    var id = that.data.id;
    console.log(that.data.region)

    app.wxRequest('POST','/Home/User/add_address',{
      'name':name,
      'mobile':mobile,
      'address':address,
      'province':province,
      'city':city,
      'area':area,
      "userId":userId,
      "status":status,
      "id":id
    },function(res){
      console.log(res);
      if(res.data.code == 1){
        wx.navigateBack({
          delta: 1 //返回上一级页面
         })
      }
    })

  }











})